<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/1
 * Time: 14:09
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Base extends Controller
{
    //用户Id
    protected $uid;

    //用户信息
    protected $userData;

    public function __construct()
    {
        parent::__construct();
        $this->info();
    }

    protected function info()
    {
        $token = input("token");
        $uid = Session::get($token . 'uid');
        if ($uid == null) {
            $redirect = "/index/login";
            $this->redirect($redirect);
        } else {
            $this->userData = Db::name("users")->alias("u")->join("__INVEST__ i", "u.level=i.id")->where("user_id", $uid)->field("u.*,i.name,i.name_en")->find();
            if (empty($this->userData)) {
                $redirect = "/index/login";
            }
        }
        $this->uid = $uid;
        $balanceData = Db::name("balance")->where("user_id", $this->uid)->find();
        if(empty($balanceData))
        {
            $res=Db::name("balance")->insert(["user_id"=>$this->uid]);
            if($res)
            {
                $balanceData = Db::name("balance")->where("user_id", $this->uid)->find();
            }
        }
        $this->assign("balanceData",$balanceData);
        $pUserInfo=Db::name("users")->where("user_id",$this->userData["pid"])->field("user_name")->find();
        $this->assign("userInfo",$this->userData);
        $this->assign("puser_name",$pUserInfo["user_name"]);
        $this->assign("think_var",cookie('think_var')?cookie('think_var'):"zh-cn");
    }
}